
(function (window) {   

    const vm = new Vue({
        el: '.app',
        data: {
           
            data:null,
            myDoctors: [],



            myPatients:[],
            constMyPatients:[],
            patientOk:false,


            notices:[],
            // 记录展示的排班表的开始结束小时，用于对json数据处理
            edgetime:{
                starthour:8,
                endhour:22
            },
            daytime:["8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00"],
            weektime:[
                {dots:""},
                {dots:""},
                {dots:""},
                {dots:""},
                {dots:""},
                {dots:""},
                {dots:""},
            ],
            



            // 弹窗data
            dialogVisible:false,
            patientId:null,
            medicineTable:[],
            haveMedicine:true,
            // medicineTable:[
            //     {id:1001,name:"阿莫西林",description:"消炎",dosage:"",check:false,warn:""},
            //     {id:1002,name:"阿司匹林",description:"xxx",dosage:"",check:false,warn:""},
            //     {id:1003,name:"999感冒灵",description:"治感冒",dosage:"",check:false,warn:""},
            //     {id:1004,name:"板蓝根",description:"治上火",dosage:"",check:false,warn:""}
            // ],
            
        },
        created() {

            const date = new Date()
            this.date = `${date.getMonth() + 1}月${date.getDate()}日`
            //获取患者列表
            axios({
                method: 'get',
                url: `http://localhost:3000/user`,
            }).then(res => {
                this.constMyPatients = res.data
                    this.myPatients=this.constMyPatients;
                    console.log(this.myPatients);
                    this.patientOk=true;
             })
            
            // axios({
            //     method: 'get',
            //     url: `http://localhost:3000/user`,
            // }).then(res => {
            //     this.constMyPatients = res.data
            //     axios({
            //         method: 'get',
            //         url: `http://localhost:3000/patient`,
            //     }).then(res => {
            //         let Patient = res.data
            //         console.log(Patient);
            //         for(let i=0;i<Patient.length;i++){
            //             this.constMyPatients[i].doctor=Patient[i].doctor;
            //         }
            //         this.myPatients=this.constMyPatients;
            //         console.log(this.myPatients);
            //         this.patientOk=true;
            //     })
            // })

            //获取消息通知
            axios({
                method: 'get',
                url: 'http://localhost:3000/newNotice',
            }).then(res => {
                this.notices = res.data
            })

            // 获取排班表
            axios({
                method: 'get',
                url: 'http://localhost:3000/worktime',
            }).then(res => {
                let time=res.data.time;
                let times=[];
                for (var i=0;i<7;i++)
                { 
                   times.push(time.substr(i*48,48).substring(this.edgetime.starthour*2,this.edgetime.endhour*2));
                   this.weektime[i].dots=times[i];
                //    对排班表进行处理
                }
            })

            //获取健康上报信息,如果今日已填，则将信息展示
            axios({
                method: "get",
                url: `http://localhost:3000/healthReport/${this.usrID}`
            }).then(res => {
                if (res.data.hasSubmitted == true) {
                    console.log(res)
                    this.hasSubmitted = true
                    this.submitBtnText = '已提交 '
                    this.healthReport[0].value = res.data.temperature
                    this.healthReport[1].value = res.data.pulse
                    this.healthReport[2].value = res.data.bloodPressure
                }
            })

            axios({
                method: 'get',
                url: 'http://localhost:3000/medicine',
            }).then(res => {
                
                this.medicineTable = res.data
                // this.medicineTable.forEach(element => {
                //     element.dosage="";
                //     element.check="false";
                //     element.warn=""
                // });
                this.medicineTable.forEach(element => {
                    this.$set(element,"dosage","");
                    this.$set(element,"check",false);
                    this.$set(element,"warn","");
                });
                this.haveMedicine=true
            })
        },
        methods: {
           

            //消息通知

            deleteNotice(index){           
                console.log(index);
                this.notices.splice(index,1);
                
            },
            location(index){
                console.log(index);            
                let thePatient=this.searchPatient(index);
                if(thePatient!=null){
                 this.myPatients=[];
                 this.myPatients.push(thePatient);
                }
               
            },
            searchPatient(index){
                let thePatient=null;
                this.constMyPatients.forEach(element => {
                    console.log(element.id);
                    if(element.id==index){
                        thePatient=element;
                        return;
                    }
                });
                return thePatient;
            },

            //刷新
            refresh(){
               this.myPatients=this.constMyPatients
            },




           //开处方
            prescribe(id){       
                 this.patientId=id;    
                 this.dialogVisible=true;                    
            }, 
            prescribeSubmit(){
               console.log(this.medicineTable);
               let selectMedicine=[];
               let select=false;
               this.medicineTable.forEach(element => {
                   if(element.check){
                       selectMedicine.push(element);
                       select=true;
                   }
               });
               if(select){
                   console.log(selectMedicine);
                   //    post请求
                   axios({
                    method: 'post',
                    url: 'http://localhost:3000/medicine',
                    data: {
                        patientId:this.patientId,
                        selectMedicine:selectMedicine
                    }
                }).then(res => {
                    console.log(res)
                }).catch(err => {
                    console.error(err);
                })
               }
               this.dialogVisible=false;
            },
        }

    })
})(window)